/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util.components;

import java.awt.Cursor;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButton;

/**
 *
 * @author Raul
 */
public class BusquedaPnl extends util.components.RoundedTitlePanel {

    private ArrayList<JRadioButton> botonesFiltros = new ArrayList<JRadioButton>();
    
    private IGestorBusqueda gestorVista;
    /**
     * Creates new form BusquedaPnl
     */
    public BusquedaPnl(){
        initComponents();
        filtrosPnl.setLayout(new FlowLayout(FlowLayout.LEADING));
        agregarFiltrosDeBusqueda(new String[]{"filtro 1", "filtro2"});
    }
    
    public BusquedaPnl(IGestorBusqueda gestorVista) {
        this();
        this.gestorVista = gestorVista;
    }
    
    public BusquedaPnl(IGestorBusqueda gestorVista, String [] filtros){
        this(gestorVista);
        agregarFiltrosDeBusqueda(filtros);
    }

    public IGestorBusqueda getGestorVista() {
        return gestorVista;
    }

    public void setGestorVista(IGestorBusqueda gestorVista) {
        this.gestorVista = gestorVista;
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        grupo = new javax.swing.ButtonGroup();
        campoBusquedaTxt = new javax.swing.JTextField();
        filtrosPnl = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        buscarBtn = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(226, 94));
        setTitle("Búsqueda");

        campoBusquedaTxt.setToolTipText("Escriba lo que desea buscar y presione Enter");
        campoBusquedaTxt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoBusquedaTxtActionPerformed(evt);
            }
        });
        campoBusquedaTxt.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                campoBusquedaTxtFocusGained(evt);
            }
        });

        filtrosPnl.setBorder(null);
        filtrosPnl.setMinimumSize(new java.awt.Dimension(105, 46));

        jRadioButton1.setText("filtro");

        javax.swing.GroupLayout filtrosPnlLayout = new javax.swing.GroupLayout(filtrosPnl);
        filtrosPnl.setLayout(filtrosPnlLayout);
        filtrosPnlLayout.setHorizontalGroup(
            filtrosPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filtrosPnlLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jRadioButton1)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        filtrosPnlLayout.setVerticalGroup(
            filtrosPnlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(filtrosPnlLayout.createSequentialGroup()
                .addComponent(jRadioButton1)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        buscarBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/zoom.png"))); // NOI18N
        buscarBtn.setText("Buscar");
        buscarBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoBusquedaTxtActionPerformed(evt);
            }
        });

        jLabel1.setText("Filtros:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(campoBusquedaTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE)
                        .addGap(14, 14, 14)
                        .addComponent(buscarBtn))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(filtrosPnl, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jLabel1)
                    .addComponent(filtrosPnl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoBusquedaTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buscarBtn)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void campoBusquedaTxtFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_campoBusquedaTxtFocusGained
        campoBusquedaTxt.selectAll();
    }//GEN-LAST:event_campoBusquedaTxtFocusGained

    private void campoBusquedaTxtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoBusquedaTxtActionPerformed
        try {
            this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            gestorVista.busquedaRapida(this.getFiltroSeleccionado(), campoBusquedaTxt.getText());
        } finally {
            this.setCursor(Cursor.getDefaultCursor());
        }
    }//GEN-LAST:event_campoBusquedaTxtActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarBtn;
    private javax.swing.JTextField campoBusquedaTxt;
    private javax.swing.JPanel filtrosPnl;
    private javax.swing.ButtonGroup grupo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JRadioButton jRadioButton1;
    // End of variables declaration//GEN-END:variables
    
    /**Limpia el panel de criterios de busqueda */
    public void limpiar(){
        //Limpia el panel
        filtrosPnl.removeAll();
        //Vacia el array
        botonesFiltros.clear();
        grupo = new ButtonGroup();
    }
    
    public void reset(){
        //Hace que se actualice la vista del panel
        filtrosPnl.revalidate();
        filtrosPnl.repaint();
        
    }
    
     /**Agrega los filtros de busqueda al panel de busqueda
     * 
     * @param filtros El vector de filtros que se agregaran al panel
     */
    public final void agregarFiltrosDeBusqueda(String [] filtros){
        limpiar();
        //Agrega los criterios
        for (int i=0 ; i<filtros.length ; i++)
            this.agregarFiltroDeBusqueda(filtros[i], grupo);
        botonesFiltros.get(0).setSelected(true);
        reset();
    }

    
    /**Agrega al panel de criterios de busqueda el <code>filtro</code> especificado
     * como un JCheckBox, ademas los agrega al array <code>checkBoxsCriterios</code>
     * 
     * @param filtro el criterio a agregar
     * @param grupo el grupo de botones radiales
     */
    private void agregarFiltroDeBusqueda(String filtro, ButtonGroup grupo){
        JRadioButton radioButton = new JRadioButton(filtro);
        botonesFiltros.add(radioButton);
        grupo.add(radioButton);
        filtrosPnl.add(radioButton);
    }
    
    /**Obtiene la lista de filtros seleccionados por el usuario para realizar
     * la busqueda
     * 
     * @return la lista de filtros que selecciono el usuario
     */
    
    public String getFiltroSeleccionado(){
        Iterator<JRadioButton> i = botonesFiltros.iterator();
        while (i.hasNext()){
            JRadioButton c = i.next();
            if (c.isSelected())
                return c.getText();
        }
    return null;
    }
     

}